package com.t2t.top.controller;

import com.t2t.top.constant.OperCodeConstants;
import com.t2t.top.exception.BizException;
import com.t2t.top.framework.base.BaseController;
import com.t2t.top.framework.model.ResponseDTO;
import com.t2t.top.framework.util.validate.Assert;
import com.t2t.top.model.dto.UserSayDto;
import com.t2t.top.model.po.User;
import com.t2t.top.model.po.UserSay;
import com.t2t.top.service.UserSayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yangpengfei
 */
@Controller
@RequestMapping("/say")
public class UserSayController extends BaseController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserSayService userSayService;

    @RequestMapping("/query")
    @ResponseBody
    public ResponseDTO query(UserSay say) throws BizException {
        try {
            Assert.isNull(say.getUserid(), "UserId不允许为null!");
            List<User> list = userSayService.query(say);
            return ResponseDTO.bulidSuccessResult().setResult(list);
        } catch (Exception e) {
            logger.error("查询失败", e);
            return ResponseDTO.bulidFailResult();
        }
    }

    @RequestMapping("/update")
    public String update(UserSay say) throws BizException {
        ResponseDTO responseDto = ResponseDTO.bulidSuccess(Thread.currentThread().getStackTrace()[1].getMethodName());
        try {
            userSayService.add(say);
        } catch (Exception e) {
            logger.error("更新失败", e);
            responseDto = ResponseDTO.bulidFailResult().setResult(e.getMessage());
            getRequest().setAttribute("dto", responseDto);
            return "common/error";
        }
        return "redirect:/jsp/say/list.jsp";
    }

    @RequestMapping("/del")
    @ResponseBody
    public ResponseDTO del(UserSayDto dto) throws BizException {
        ResponseDTO responseDto = ResponseDTO.bulidSuccess(Thread.currentThread().getStackTrace()[1].getMethodName());
        try {
            UserSay say = new UserSay();
            userSayService.delete(say);
        } catch (BizException e) {
            logger.error("删除失败", e);
            return ResponseDTO.bulidFailResult().setResult(e.getMessage());
        }
        return responseDto.setResult(OperCodeConstants.SAY_DEL_SECCESS);
    }


}
